#!/bin/sh

# @file ralf.monit
#
# Copyright (C) Metaswitch Networks 2017
# If license terms are provided to you in a COPYING file in the root directory
# of the source code repository by which you are accessing this code, then
# the license outlined in that COPYING file applies to your use.
# Otherwise no rights are granted except for those provided to you by
# Metaswitch Networks in a separate written agreement.

. /etc/clearwater/config

if [ -z "$ralf_hostname" ]; then

  if [ -f /etc/monit/conf.d/ralf.monit ]; then
    rm /etc/monit/conf.d/ralf.monit

    # Force monit to reload its configuration
    reload clearwater-monit || true

    # Stop ralf if it was running
    service ralf stop
  fi

else

  # Set up the monit configuration for ralf with the right IP addresses and ports
  cat > /etc/monit/conf.d/ralf.monit <<EOF
# Check the Ralf process.

# Monitor the service's PID file and memory use.
check process ralf_process with pidfile /var/run/ralf/ralf.pid
  group ralf

  # The start, stop and restart commands are linked to alarms
  start program = "/bin/bash -c '/usr/share/clearwater/infrastructure/monit_stability/ralf-stability reset; /usr/share/clearwater/bin/issue-alarm monit 2000.3; /etc/init.d/ralf start'"
  stop program = "/bin/bash -c '/usr/share/clearwater/bin/issue-alarm monit 2000.3; /etc/init.d/ralf stop'"
  restart program = "/bin/bash -c '/usr/share/clearwater/infrastructure/monit_stability/ralf-stability reset; /usr/share/clearwater/bin/issue-alarm monit 2000.3; /etc/init.d/ralf restart'"

  # Check the service's resource usage, and abort the process if it's too high. This will
  # generate a core file and trigger diagnostics collection.
  if memory > 40% then exec "/bin/bash -c '/usr/share/clearwater/infrastructure/monit_stability/ralf-stability aborted; /usr/share/clearwater/bin/issue-alarm monit 2000.3; /etc/init.d/ralf abort'"

# Clear any alarms if the process has been running long enough.
check program ralf_uptime with path /usr/share/clearwater/infrastructure/monit_uptime/check-ralf-uptime
  group ralf
  depends on ralf_process
  every 3 cycles
  if status != 0 then alert

# Check the HTTP interface. This depends on the Ralf process (and so won't run
# unless the Ralf process is running)
check program poll_ralf with path "/usr/share/clearwater/bin/poll_ralf.sh"
  group ralf
  depends on ralf_process

  # Aborting generates a core file and triggers diagnostic collection.
  if status != 0 for 2 cycles then exec "/bin/bash -c '/usr/share/clearwater/infrastructure/monit_stability/ralf-stability aborted; /usr/share/clearwater/bin/issue-alarm monit 2000.3; /etc/init.d/ralf abort'"
EOF

  chmod 0644 /etc/monit/conf.d/ralf.monit

  # Force monit to reload its configuration
  reload clearwater-monit || true

fi
